package router

import (
	"net/http"

	"server/middleware"
	v1 "server/router/api/v1"

	"github.com/gin-gonic/gin"
	ginSwagger "github.com/swaggo/gin-swagger"
	"github.com/swaggo/gin-swagger/swaggerFiles"
)

func InitRouterUser(Router *gin.RouterGroup) {
	UserRouter := Router.Group("")
	{
		UserRouter.POST("user/login", v1.UserLogin)
		UserRouter.POST("user/logout", v1.UserLogout)
		UserRouter.GET("user/info", v1.UserInfo)
		UserRouter.POST("user/register", v1.UserRegister)

		UserRouter.GET("users", v1.GetUsers)
		UserRouter.GET("users/:username", v1.GetUser)
		UserRouter.PUT("users", v1.EditUser)
		UserRouter.DELETE("users/:username", v1.DeleteUser)
	}
}

func InitRouterDevice(Router *gin.RouterGroup) {
	DeviceRouter := Router.Group("").Use(middleware.JWT())
	{
		DeviceRouter.POST("device", v1.AddDevice)
		DeviceRouter.PUT("device", v1.EditDevice)
		DeviceRouter.DELETE("device/:id", v1.DeleteDevice)
		DeviceRouter.GET("device/:id", v1.GetDevice)
		DeviceRouter.GET("devices", v1.GetDevices)
		DeviceRouter.GET("device_logs", v1.GetDeviceLogs)
	}
}

func InitRouterAuto(Router *gin.RouterGroup) {
	AutoRouter := Router.Group("").Use(middleware.JWT())
	{
		AutoRouter.POST("auto", v1.AddAuto)
		AutoRouter.PUT("auto", v1.EditAuto)
		AutoRouter.DELETE("auto/:id", v1.DeleteAuto)
		AutoRouter.GET("auto/:id", v1.GetAuto)
		AutoRouter.GET("autos", v1.GetAutos)
	}
}

func InitRouterEmqx(Router *gin.RouterGroup) {
	EmqxRouter := Router.Group("emqx")
	{
		EmqxRouter.GET("check_online", v1.EmqxCheckOnline)
	}
}

func InitRouterUpload(Router *gin.RouterGroup) {
	uploadRouter := Router.Group("upload")
	{
		uploadRouter.POST("bin", v1.UploadBin)
		uploadRouter.POST("image", v1.UploadBin)
	}
}

// InitRouter initialize routing information
func InitRouter() *gin.Engine {
	r := gin.Default()
	// r.Use(gin.Logger())
	// r.Use(gin.Recovery())

	// swagger文档
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))
	// 静态文件
	r.StaticFS("/upload", http.Dir("./upload"))
	// 日志
	r.StaticFS("/log", http.Dir("./log"))

	// v1
	ApiGroup := r.Group("/api")
	InitRouterUser(ApiGroup)
	InitRouterDevice(ApiGroup)
	InitRouterAuto(ApiGroup)
	InitRouterEmqx(ApiGroup)
	InitRouterUpload(ApiGroup)

	return r
}
